# coding=utf-8
from flask_mail import Message
from app import mail, application
from app.decorators import async
from config import ADMINS
from flask import render_template


# 异步方式发送邮件
@async
def send_async_email(application, msg):
    with application.context():
        mail.send(msg)

    print '-------->>> send mail successful!'


# 发送邮件
def send_email(subject, sender, recipients, text_body, html_body):
    msg = Message(subject, sender=sender, recipients=recipients)
    msg.body = text_body
    msg.html = html_body
    # 开启一个新的线程发送邮件，防止网页堵塞
    send_async_email(application, msg)


# 用户关注提醒
def follower_notification(followed, follower):
    send_email("[Micro-blog] %s is now following you!" % follower.nickname,
               ADMINS[0], [followed.email],
               render_template('follower_email.txt', user=followed, follower=follower),
               render_template('follower_email.html', user=followed, follower=follower))
